package com.tianbing.adlottery.controller.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.jubao.ejb.business.UserBusiness;
import com.jubao.ejb.dao.UserDao;
import com.jubao.ejb.dao.WebsiteDao;
import com.jubao.ejb.entity.User;
import com.jubao.ejb.entity.UserWebsite;
import com.jubao.ejb.entity.Website;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class EditFavoriteWebsiteController extends SimpleFormController {
	static Logger logger = Logger.getLogger(EditFavoriteWebsiteController.class.getName());

	private SkinUtils skinUtils;

	private UserBusiness userBusiness;

	private WebsiteDao websiteDao;

	private UserDao userDao;

	// IPartyService partyService;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		UserSession auth = (UserSession) skinUtils.getUserAuthorization(request, null, true);
		long wbsid = ServletRequestUtils.getLongParameter(request, "wbsid", -1);
		Website website = websiteDao.find(wbsid);
		ControllerFormBean fb = new ControllerFormBean();
		UserWebsite userWebsite = null;
		if (website != null) {
			userWebsite = userBusiness.findFavoriteWebsite(auth.getUserSysId(), wbsid);
			if (userWebsite == null) {
				User user = userDao.find(auth.getUserSysId());
				userWebsite = new UserWebsite();
				userWebsite.setUser(user);
				userWebsite.setWebsite(website);
			}
		}
		fb.setUserWebsite(userWebsite);
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		ModelAndView mv = new ModelAndView(this.getSuccessView());
		ControllerFormBean form = (ControllerFormBean) cmd;
		UserSession auth = (UserSession) skinUtils.getUserAuthorization(request, response, true);
		UserWebsite userWebSite = form.getUserWebsite();
		long wbsid = ServletRequestUtils.getLongParameter(request, "wbsid", -1);
		userBusiness.saveFavoriteWebsite(auth.getUserSysId(), wbsid, userWebSite);
		return mv;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	/**
	 * @param userBusiness the userBusiness to set
	 */
	public void setUserBusiness(UserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	/**
	 * @param websiteDao the websiteDao to set
	 */
	public void setWebsiteDao(WebsiteDao websiteDao) {
		this.websiteDao = websiteDao;
	}

	/**
	 * @param userDao the userDao to set
	 */
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

}
